package cn.lopr.im.android.mvp.model;

import android.text.TextUtils;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.Map;

import cn.lopr.im.android.bean.CallbackResult;
import cn.lopr.im.android.bean.User;
import cn.lopr.im.android.common.Constant;
import cn.lopr.im.android.common.OnCallBackListener;
import cn.lopr.im.android.mvp.contact.IRegisterBeforeContact;
import cn.lopr.im.android.utils.Apis;
import cn.lopr.im.android.utils.GsonUtil;
import cn.lopr.im.android.utils.HttpUtils;
import cn.lopr.im.android.utils.cos.CosProperties;

/**
 * @author shuangyang7
 * @date 2020/12/19 11:51
 */
public class RegisterBeforeModel implements IRegisterBeforeContact.IRegisterBeforeModel {

    @Override
    public void getCode(String phone, OnCallBackListener<CallbackResult<User>, String> onCallBackListener) {
        Log.i("ContactModel", Apis.WebApis.GET_FRIEND.getValue());
        String param = String.format("?phone=%s", phone);
        HttpUtils.doGetForResult(Apis.WebApis.GET_CODE.getValue(), param, data -> {
            Log.d("json", data);

            if (TextUtils.isEmpty(data)) {
                onCallBackListener.failure(Constant.CALLBACK_ERROR_MESSAGE);
                return;
            }

            Type jsonType = new TypeToken<CallbackResult<User>>() {
            }.getType();
            CallbackResult<User> callbackResult = new Gson().fromJson(data, jsonType);

            if (callbackResult.getStatus() == Constant.ResponseCode.SUCCESS.getCode()) {
                onCallBackListener.success(callbackResult);

            } else {
                onCallBackListener.failure(callbackResult.getMsg());

            }
        });
    }

    @Override
    public void regBefore(String phone, String password, String sex, String portrait, String introduction, OnCallBackListener<CallbackResult<Map<String, Object>>, String> onCallBackListener) {
        String param = String.format("?phone=%s&password=%s&sex=%s&portrait=%s&introduction=%s", phone, password, sex, portrait, introduction);
        Log.i("json", "regBefore: " + param);
        HttpUtils.doGetForResult(Apis.WebApis.REG_BEFORE.getValue(), param, data -> {
            Log.d("json", data);

            if (TextUtils.isEmpty(data)) {
                onCallBackListener.failure("");
                return;
            }

            Type jsonType = new TypeToken<CallbackResult<Map<String, Object>>>() {
            }.getType();
            CallbackResult<Map<String, Object>> callbackResult = new Gson().fromJson(data, jsonType);

            if (callbackResult.getStatus() == Constant.ResponseCode.SUCCESS.getCode()) {
                onCallBackListener.success(callbackResult);

            } else {
                onCallBackListener.failure(callbackResult.getMsg());

            }
        });
    }

    @Override
    public void getCosTempSecret(OnCallBackListener<CosProperties, String> onCallBackListener) {
        HttpUtils.doGetForResult(Apis.WebApis.COS_TEMP_SECRET.getValue(), data -> {
            Log.d("json", data);

            if (TextUtils.isEmpty(data)) {
                onCallBackListener.failure("");
                return;
            }

            Type jsonType = new TypeToken<CallbackResult<Map<String, Object>>>() {
            }.getType();
            CallbackResult<Map<String, Object>> callbackResult = new Gson().fromJson(data, jsonType);

            if (callbackResult.getStatus() == Constant.ResponseCode.SUCCESS.getCode()) {
                Map<String, Object> resData = callbackResult.getData();

                onCallBackListener.success(GsonUtil.mapToObject(resData, CosProperties.class));

            } else {
                onCallBackListener.failure(callbackResult.getMsg());

            }
        });
    }


}
